
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;

public class entrada extends javax.swing.JFrame {

    Socket yo = null;
    Correos principal;

    public entrada(Correos principal) {
        initComponents();
        this.setSize(400, 400);
        this.principal = principal;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setText("User");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 300, -1, -1));
        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 320, 120, -1));

        jLabel3.setText("Password");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 300, -1, -1));

        jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField1ActionPerformed(evt);
            }
        });
        getContentPane().add(jPasswordField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(230, 320, 110, -1));

        jButton1.setText("Entrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 250, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Universidad Autonoma de Guerrero");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 10, 300, 30));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Unidad Academica de Ingenieria");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 50, 270, 20));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mail.png"))); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jPasswordField1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (jPasswordField1.getText().length() > 0 && jTextField1.getText().length() > 0) {

            if (yo == null) {
                try {
                    // TODO add your handling code here:

                    try {
                        yo = new Socket("", 5500);

                    } catch (UnknownHostException e) {
                        System.out.println(e.getMessage());
                        System.exit(1);
                         JOptionPane.showMessageDialog(null, "Problemas con la conexion");
                         yo=null;
                    } catch (IOException ex) {
                        System.err.println(ex.getMessage());
                        JOptionPane.showMessageDialog(null, "Problemas con la conexion");
                     yo=null;
                    }

                    if (yo != null) {

                        PrintWriter sal = new PrintWriter(yo.getOutputStream(), true);
                        DataInputStream delServidor = new DataInputStream(yo.getInputStream());
                        DataOutputStream alServidor = new DataOutputStream(yo.getOutputStream());

                        sal.println(jTextField1.getText());
                        sal.println(jTextField1.getText());
                        boolean respuesta = delServidor.readBoolean();

                        System.out.println("recibi comprobacion del servidor :" + respuesta);
                        if (respuesta) {
                            this.setVisible(false);
                            principal.setVisible(true);
                            principal.setUser(yo);
                            principal.setDIS(delServidor);
                            principal.setDOS(alServidor);
                            principal.setPW(sal);
                            principal.setUserName(jTextField1.getText());
                            principal.actualizaCorreosRecibidos();
                            principal.actualizaCorreosEnviados();

                            jTextField1.setText("");
                            jPasswordField1.setText("");
                            
                            System.out.println("enviado");

                        } else {

                            alServidor.writeUTF("");
                            delServidor.close();
                            alServidor.close();
                            sal.close();
                            yo.close();
                            yo = null;

                            JOptionPane.showMessageDialog(null, "Datos incorrectos");
                        }

                    }

                } catch (IOException e) {
                    System.err.println(e.getMessage());
                    System.exit(1);
                }

            }
        }


    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
